package com.greateamd.timetableasy.client.widgets.room;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.greateamd.timetableasy.client.widgets.Grid;
import com.greateamd.timetableasy.client.widgets.ListBox;
import com.greateamd.timetableasy.domain.Room;

public class ListRoomBuilder extends Composite {

	@UiTemplate("ListRoomBuilder.ui.xml")
	interface ListRoomBuilderUIBinder extends UiBinder<Widget, ListRoomBuilder> {}
	private static ListRoomBuilderUIBinder uiBinder = GWT.create(ListRoomBuilderUIBinder.class);
	
	private List<Room> listOfRooms;
	
	@UiField ListBox list;
	@UiField Button addBt;
	@UiField Button removeBt;
	
	// Popup
	private DialogBox dialogBox;
	private TextBox nameTb;
	private CheckBox enabledCb;
	private Button popupSubmitBt;
	private Button popupCloseBt;
	
	public ListRoomBuilder() {
		initWidget(uiBinder.createAndBindUi(this));
		listOfRooms = new ArrayList<Room>();
		popupCloseBt = new Button("Close", popupCloseHandler);
		popupSubmitBt = new Button("Add", popupSubmitHandler);
	}
	
	private ClickHandler popupSubmitHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent arg0) {
			Room room = new Room(nameTb.getText(),null);
			listOfRooms.add(room);
			list.addItem(room.getName());
		}
	};
	private ClickHandler popupCloseHandler = new ClickHandler() {
		@Override
		public void onClick(ClickEvent arg0) {
			dialogBox.hide();			
		}
	};
	
	//---------------------------------------------------------------------
	//-------------------------- Handler
	@UiHandler("addBt")
	void onAddRoomBtClick(ClickEvent e){
		dialogBox = new DialogBox(false,true);
	    dialogBox.setGlassEnabled(true);
	    dialogBox.setAnimationEnabled(true);
	    dialogBox.setText("Add a room");
	    dialogBox.setWidget(buildAddRoomForm());
	    dialogBox.center();
	    dialogBox.show();
	}

	@UiHandler("removeBt")
	void onRemoveRoomBtClick(ClickEvent e){
		if(list.getSelectedIndex() != -1){
			list.removeItem(list.getSelectedIndex());
			listOfRooms.remove(list.getSelectedIndex());
		}
	}
	

	//---------------------------------------------------------------------
	//-------------------------- UI action
	
	private Widget buildAddRoomForm() {
		nameTb = new TextBox();
		enabledCb = new CheckBox();
		Grid grid = new Grid(3,4);
		grid.setWidget(0, 0, new Label("Name :"));
		grid.setWidget(0, 1, nameTb);
		grid.setWidget(1, 0, new Label("Enabled :"));
		grid.setWidget(1, 1, enabledCb);
		grid.setWidget(2, 0, popupCloseBt);
		grid.setWidget(2, 1, popupSubmitBt);
		return grid;
	}

	public List<Room> getListOfRooms() {
		return listOfRooms;
	}
}
